import logging

from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect
from portal.lib.auth import private_page
from portal.lib.base import BaseController, render
from portal.model import News, Page, Spectacle, Evenement, Programmation
from portal import model
from pylons import config
import cgi

import os

log = logging.getLogger(__name__)

class DashboardController(BaseController):

    #UPLOAD_DIR = config.get('upload_dir')
    UPLOAD_DIR = "/home/kenshuri/c_chrysalide/trunk/portal/portal/public/upload/"
	
    def index(self):
        return private_page('/dashboard/index.mako')

    
    def upload(self):
        c.files =  os.listdir(self.UPLOAD_DIR)
        return private_page("/dashboard/upload.mako")


    
    def upload_submit(self):
        file_save_path = os.path.join(self.UPLOAD_DIR, request.POST['myfile'].filename)

        file_contents = request.POST['myfile'].file.read()
    
        try:
            save_file = open(file_save_path, 'wb')
        except Exception, e:
            return redirect('/dashboard/upload?upload=failed')
        
        save_file.write(file_contents)
        save_file.close()

        return redirect("/dashboard/upload?upload=success")
            
            
    def delete_uploaded_file(self, file_name):
        log.debug("removing file")
        
        try:
            os.remove(self.UPLOAD_DIR + file_name)
        except Exception, e:
            log.debug("removing file failed \n "+str(e))
            return redirect('/dashboard/upload?delete=failed')
        
        log.debug("removing file success")
        
        return redirect('/dashboard/upload?delete=success')
            
    
    def add_news(self):
        return private_page('/dashboard/news/add_news.mako')
    
    def add_news_submit(self):
        title = request.params.get('title')
        content = request.params.get('content')
        if title != '' and content != '' and title is not None and content is not None:
            News().add_news(title, content)
            c.message = "<span class='success_message'>La news a ete correctement ajoutee</span>"
        else:
            c.message = "<span class='error_message'>La news n'a pas ete ajoutee - tout les champs sont requit</span>"    
        return private_page('/dashboard/news/add_news.mako')
    
    
    def modif_news(self):
        q = model.Session.query(model.News)
        c.news = q.all()
        return private_page('/dashboard/news/modif_news.mako')

    
    def modif_news_id(self, id):
        q = model.Session.query(model.News).filter(model.News.id == id)
        c.news = q.first()
        return private_page('/dashboard/news/modif_news_id.mako')

    
    def modif_news_submit(self):
        title = request.params.get('title')
        content = request.params.get('content')
        id = request.params.get('id')
        if title != '' and content != '' and title is not None and content is not None:
            News().update_news(id, title, content)
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/modif_news?modified='+res)

    
    def del_news(self):
        q = model.Session.query(model.News)
        c.news = q.all()
        return private_page('/dashboard/news/del_news.mako')

    
    def del_news_id(self, id):
        q = model.Session.query(model.News).filter_by(id = id)
        if q.delete():
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/del_news?deleted='+res)    
    
    
    def modif_page(self, page):
        q = model.Session.query(model.Page).filter(model.Page.name == page)
        c.page = q.first()
    
        return private_page('/dashboard/modif_page.mako')
    
    
    def modif_page_submit(self):
        content = request.params.get('content')
        id = request.params.get('id')
        name = str(request.params.get('name'))
        if content != '' and content is not None:
            Page().update_page(id, content)
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/modif_page/'+name+'?modified_page='+res)
    
    
    def add_spectacle(self):
        return private_page('/dashboard/spectacle/add_spectacle.mako')
    
    
    def add_spectacle_submit(self):
        title = request.params.get('title')
        content = request.params.get('content')
        if title != '' and content != '' and title is not None and content is not None:
            Spectacle().add_spectacle(title, content)
            c.message = "<span class='success_message'>Le spectacle a ete correctement ajoutee</span>"
        else:
            c.message = "<span class='error_message'>Le spectacle n'a pas ete ajoutee - tout les champs sont requit</span>"    
        return private_page('/dashboard/spectacle/add_spectacle.mako')
    
    
    def modif_spectacle(self):
        q = model.Session.query(model.Spectacle)
        c.spectacles = q.all()
        return private_page('/dashboard/spectacle/modif_spectacle.mako')
    
    
    def modif_spectacle_id(self, id):
        q = model.Session.query(model.Spectacle).filter(model.Spectacle.id == id)
        c.spectacle = q.first()
        return private_page('/dashboard/spectacle/modif_spectacle_id.mako')


    def modif_spectacle_submit(self):
        title = request.params.get('title')
        content = request.params.get('content')
        log.debug('########################################' + title)
        id = request.params.get('id')
        if title != '' and content != '' and title is not None and content is not None:
            Spectacle().update_spectacle(id, title, content)
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/modif_spectacle?modified='+res)

    
    def del_spectacle(self):
        q = model.Session.query(model.Spectacle)
        c.spectacles = q.all()
        return private_page('/dashboard/spectacle/del_spectacle.mako')

    
    def del_spectacle_id(self, id):
        q = model.Session.query(model.Spectacle).filter_by(id = id)
        if q.delete():
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/del_spectacle?deleted='+res)

    
    def add_evenement(self):
        return private_page('/dashboard/evenement/add_evenement.mako')

    
    def add_evenement_submit(self):
        name = request.params.get('title')
        content = request.params.get('content')
        if name != '' and content != '' and name is not None and content is not None:
            Evenement().add_evenement(name, content)
            c.message = "<span class='success_message'>L'evenement a ete correctement ajoutee</span>"
        else:
            c.message = "<span class='error_message'>L'evenement n'a pas ete ajoutee - tout les champs sont requit</span>"    
        return private_page('/dashboard/evenement/add_evenement.mako')

    
    def modif_evenement(self):
        
        q = model.Session.query(model.Evenement)
        c.evenements = q.all()
        return private_page('/dashboard/evenement/modif_evenement.mako')
    
    def modif_evenement_id(self, id):
        q = model.Session.query(model.Evenement).filter(model.Evenement.id == id)
        c.evenement = q.first()
        return private_page('/dashboard/evenement/modif_evenement_id.mako')
   
   
    def modif_evenement_submit(self):
        name = request.params.get('title')
        content = request.params.get('content')
        log.debug('########################################' + name)
        id = request.params.get('id')
        if name != '' and content != '' and name is not None and content is not None:
            Evenement().update_evenement(id, name, content)
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/modif_evenement?modified='+res)
    
    
    def del_evenement(self):
        q = model.Session.query(model.Evenement)
        c.evenements = q.all()
        return private_page('/dashboard/evenement/del_evenement.mako')

    
    def del_evenement_id(self, id):
        q = model.Session.query(model.Evenement).filter_by(id = id)
        if q.delete():
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/del_evenement?deleted='+res)

   
    def add_programmation(self):
        return private_page('/dashboard/programmation/add_programmation.mako')

    
    def add_programmation_submit(self):
        title = request.params.get('title')
        content = request.params.get('content')
        if title != '' and content != '' and title is not None and content is not None:
            Programmation().add_programmation(title, content)
            c.message = "<span class='success_message'>La programmation a ete correctement ajoutee</span>"
        else:
            c.message = "<span class='error_message'>La programmation n'a pas ete ajoutee - tout les champs sont requit</span>"    
        return private_page('/dashboard/programmation/add_programmation.mako')

    
    def modif_programmation(self):
        q = model.Session.query(model.Programmation)
        c.programmations = q.all()
        return private_page('/dashboard/programmation/modif_programmation.mako')

    
    def modif_programmation_id(self, id):
        q = model.Session.query(model.Programmation).filter(model.Programmation.id == id)
        c.programmation = q.first()
        return private_page('/dashboard/programmation/modif_programmation_id.mako')

    
    def modif_programmation_submit(self):
        title = request.params.get('title')
        content = request.params.get('content')
        log.debug('########################################' + title)
        id = request.params.get('id')
        if title != '' and content != '' and title is not None and content is not None:
            Programmation().update_programmation(id, title, content)
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/modif_programmation?modified='+res)

    
    def del_programmation(self):
        q = model.Session.query(model.Programmation)
        c.programmations = q.all()
        return private_page('/dashboard/programmation/del_programmation.mako')

    
    def del_programmation_id(self, id):
        q = model.Session.query(model.Programmation).filter_by(id = id)
        if q.delete():
            res = 'yes'
        else:
            res = "no"    
        return redirect('/dashboard/del_programmation?deleted='+res)
